define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	
	require("$UI/system/lib/cordova/cordova");
	require("cordova!cn.jpush.phonegap.JPushPlugin");
	
	
	var Model = function(){
		this.callParent();
		
	};
	
	Model.prototype.isShowAdvance = true;
	
	Model.prototype.modelLoad = function(event){
		
		var self = this;
		
		self.initBrowser();
	};
	
	
	Model.prototype.initBrowser = function(){
		var self = this;
		
		if (justep.Browser.isX5App){
			$(self.getElementByXid("borwserContent")).hide();
			$(self.getElementByXid("appContent")).show();
			$(self.getElementByXid("registrationOutput")).html("无数据");
			document.addEventListener("jpush.setTagsWithAlias", self.onTagsWithAlias(), false);
			document.addEventListener("jpush.openNotification", self.onOpenNotification(), false);
			document.addEventListener("jpush.receiveNotification", self.onReceiveNotification(), false);
			document.addEventListener("jpush.receiveMessage", self.onReceiveMessage(), false);
			self.initAdvance();
			
			
		}else{
			$(self.getElementByXid("appContent")).hide();
			$(self.getElementByXid("borwserContent")).hide();
			self.initAdvance();
			
		}
	};
	
	Model.prototype.initAdvance = function(){
	
		var self = this;
		self.isShowAdvance = false;
		$(self.getElementByXid("tagRow")).hide();
		$(self.getElementByXid("aliasRow")).hide();
		$(self.getElementByXid("sendRow")).hide();
		
		$(self.getElementByXid("advanceButton")).html("显示高级选项");
	};
	
	Model.prototype.onGetRegistradionID = function(data){
		var self = this;
		try{
			$(self.getElementByXid("registrationOutput")).html(data);
		}catch(e){
			
		}
	};
	
	Model.prototype.initJpushForApp = function(){
		try{
			window.plugins.jPushPlugin.init();
			window.plugins.jPushPlugin.getRegistrationID(this.onGetRegistradionID());
		}catch(e){
		}
		
		if(justep.Browser.isAndroid){
			 window.plugins.jPushPlugin.setDebugModeFromIos();
             window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
		}else{
			 window.plugins.jPushPlugin.setDebugMode(true);
		}
	};
	
	Model.prototype.onTagsWithAlias = function(event){
	    try{
	        var result="result code: "+event.resultCode+" ";
	        result+="tags:"+event.tags+" ";
	        result+="alias:"+event.alias+" ";
	    }
	    catch(exception){
	    }
	};
	
	Model.prototype.onOpenNotification = function(event){
        try{
            var alertContent;
            if(justep.Browser.isAndroid){
                alertContent=window.plugins.jPushPlugin.openNotification.alert;
            }else{
                alertContent   = event.aps.alert;
            }
        }
        catch(exception){
        }
    };
    
    Model.prototype.onReceiveNotification = function(event){
        try{
            
            var eventContent="{";
            for(var key in event){
                if(key=="type"){
                    break;
                }
               eventContent+=key+":"+JSON.stringify(event[key])+"\n";
            }
            eventContent+="}";
            
        }
        catch(exeption){
        }
    };
    
   Model.prototype.onReceiveMessage = function(event){
        try{
             var message = window.plugins.jPushPlugin.receiveMessage.message;
              console.log("JPushPlugin:onReceiveMessage-->"+message);
        }
        catch(exception){
            console.log("JPushPlugin:onReceiveMessage-->"+exception);
        }
    };
	
	
	
	Model.prototype.advanceButtonClick = function(event){
		var self = this;
		if(self.isShowAdvance){
			self.isShowAdvance = false;
			$(self.getElementByXid("tagRow")).hide();
			$(self.getElementByXid("aliasRow")).hide();
			$(self.getElementByXid("sendRow")).hide();
			$(self.getElementByXid("advanceButton")).html("显示高级选项");
		}else{
			self.isShowAdvance = true;
			$(self.getElementByXid("tagRow")).show();
			$(self.getElementByXid("aliasRow")).show();
			$(self.getElementByXid("sendRow")).show();
			$(self.getElementByXid("advanceButton")).html("隐藏高级选项");
		}
	};
	
	
	
	Model.prototype.sendInfoButtonClick = function(event){
		var self = this;
		var tagValue = $(self.getElementByXid("tagTextArea")).val();
		var aliasValue = $(self.getElementByXid("aliasTextArea")).val();
		
		window.plugins.jPushPlugin.setTagsWithAlias(tagValue,aliasValue);
	};
	
	
	
	Model.prototype.initPushButtonClick = function(event){
		var self = this;
		self.initJpushForApp();
	};
	
	
	
	Model.prototype.stopPushButtonClick = function(event){
		window.plugins.jPushPlugin.stopPush();
	};
	
	
	
	Model.prototype.resumePushButtonClick = function(event){
		window.plugins.jPushPlugin.resumePush();
	};
	
	
	
	return Model;
});